﻿using System;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Library.Utilities;

namespace DLV.BackEnd.Pages
{
    public partial class EditTicket : AdminPageBase
    {
        protected int Type = 0;
        private TicketInfo _ticketInfo = new TicketInfo();
        private readonly TicketController _ticketController = new TicketController();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(Request.QueryString["TicketId"]))
                _ticketInfo = _ticketController.GetTicket(int.Parse(Request.QueryString["TicketId"]));

            if (!String.IsNullOrEmpty(Request.QueryString["Type"]))
                Type = int.Parse(Request.QueryString["Type"]);

            if (Page.IsPostBack) return;
                LoadUser();
                
               
                LoadConcern();
                
                LoadStatus();
            
                LoadSchedule();

            if (_ticketInfo.TicketId == -1) return;
            RenderForm();
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            SaveData();
        }
        protected void lbtClose_Click(object sender, EventArgs e)
        {
            RedirectLink();
        }

        private void SaveData()
        {
            _ticketInfo.TicketName = txtTicketName.Text;
            _ticketInfo.Status = int.Parse(ddlStatus.SelectedValue);
            _ticketInfo.ConcernId = int.Parse(ddlConcern.SelectedValue);
            _ticketInfo.SortDescription = txtSortDesription.Text;
            _ticketInfo.DetailDescription = txtContent.Text;
            _ticketInfo.ScheduleId = int.Parse(ddlScheduleId.SelectedValue);
            _ticketInfo.TypeId = Type;
            if (_ticketInfo.TicketId == -1)
            {
                _ticketInfo.CreatedBy = ddlCreatedBy.SelectedValue == "-1"
                    ? UserId
                    : int.Parse(ddlCreatedBy.SelectedValue);
                _ticketInfo.CreatedDate = DateTime.Now;
                _ticketController.InsertTicket(_ticketInfo);
            }
            else
            {
                _ticketInfo.UpdatedBy = UserId;
                _ticketInfo.UpdatedDate = DateTime.Now;
                _ticketController.UpdateTicket(_ticketInfo);
            }

            RedirectLink();
        }

        private void RedirectLink()
        {
            var link = String.Empty;
            switch (Type)
            {
                case 1:
                    link = "/Pages/ListTicket.aspx?xml=TicketAir&Type=" + Type;
                    break;
                case 2:
                    link = "/Pages/ListTicket.aspx?xml=TicketStation&Type=" + Type;
                    break;
                case 3:
                    link = "/Pages/ListTicket.aspx?xml=TicketCar&Type=" + Type;
                    break;
            }
            Response.Redirect(link);
        }

        private void RenderForm()
        {
            txtSortDesription.Text = _ticketInfo.SortDescription;
            txtContent.Text = _ticketInfo.DetailDescription;
            ddlStatus.SelectedValue = _ticketInfo.Status.ToString();
            ddlCreatedBy.SelectedValue = _ticketInfo.CreatedBy.ToString();
            ddlConcern.SelectedValue = _ticketInfo.ConcernId.ToString();
            ddlScheduleId.SelectedValue = _ticketInfo.ScheduleId.ToString();
           
            txtTicketName.Text = _ticketInfo.TicketName;
          ;
        }
        private void LoadConcern()
        {
            ddlConcern.DataSource =
               new ConcernController().ListConcernByType(Type)
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use)
                   .OrderBy(p => p.ConcernFullName);
            ddlConcern.DataTextField = "ConcernFullName";
            ddlConcern.DataValueField = "ConcernId";
            ddlConcern.DataBind();

            ddlConcern.Items.Insert(0, new ListItem("--- Chọn hãng ---", "-1"));
        }
       
        private void LoadSchedule()
        {
           if(Type==1||Type==2){
               ddlScheduleId.DataSource =
               new ScheduleController().ListSchedule()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use && p.ScheduleType==Type)
                   .OrderBy(p => p.ScheduleName);
           }
           else
           {
               ddlScheduleId.DataSource =
               new ScheduleController().ListSchedule()
                   .Where(p => p.Status == (int)Globals.DocumentStatus.Use && p.ScheduleType == 4)
                   .OrderBy(p => p.ScheduleName);
           }
            ddlScheduleId.DataTextField = "ScheduleName";
            ddlScheduleId.DataValueField = "ScheduleId";
            ddlScheduleId.DataBind();

            ddlScheduleId.Items.Insert(0, new ListItem("--- Chọn lịch trình ---", "-1"));
        }
       
        private void LoadStatus()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/Status.xml"));
            var dv = ds.Tables["Status"].DefaultView;
            ddlStatus.DataTextField = "Name";
            ddlStatus.DataValueField = "ID";
            ddlStatus.DataSource = dv;
            ddlStatus.DataBind();
        }
        //private void LoadYear()
        //{
        //    int year = DateTime.Now.Year;
        //    for (int i = 0; i>=-20; i--)
        //    {
        //        ddltRegisterYear.Items.Add((year + i).ToString());
        //    }
        //    ddltRegisterYear.Items.Insert(0, new ListItem("--- Chọn năm ---", "-1"));
        //}
        private void LoadUser()
        {
            ddlCreatedBy.DataSource = new AdminUserController().ListAdminUser();
            ddlCreatedBy.DataTextField = "UserName";
            ddlCreatedBy.DataValueField = "UserId";
            ddlCreatedBy.DataBind();

            ddlCreatedBy.Items.Insert(0, new ListItem("--- Chọn người tạo ---", "-1"));
        }
    }
}